/**
 * Created on May 12, 2007
 */
package jvs.vfs.tools;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

/**
 * @author qiangli
 * 
 */
public class MessageDialog {

	public static final int APPROVE_OPTION = BasicDialog.APPROVE_OPTION ;
	public static final int CANCEL_OPTION = BasicDialog.CANCEL_OPTION;

	private MessageDialog() {

	}

	static class ConfirmDialog extends BasicDialog {

		private static final long serialVersionUID = 1L;

		private JLabel message;

		public ConfirmDialog(Dialog owner, boolean modal) {
			super(owner, modal);
			buildUI();
		}

		public ConfirmDialog(Frame owner, boolean modal) {
			super(owner, modal);
			buildUI();
		}

		protected void buildUI() {
			JPanel custom = new JPanel();
			message = new JLabel("");
			message.setFont(new Font("Times New Roman", Font.PLAIN, 14));
			message.setHorizontalAlignment(SwingConstants.CENTER);
			custom.setLayout(new BorderLayout());
			custom.add(message, BorderLayout.CENTER);
			custom.setPreferredSize(new Dimension(400, 100));

			JPanel buttons = new JPanel();
			buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
			buttons.add(Box.createHorizontalGlue());

			okay = new JButton("Yes");
			okay.setEnabled(true);
			buttons.add(okay);
			buttons.add(Box.createRigidArea(HGAP10));

			cancel = new JButton("No");
			cancel.setEnabled(true);
			buttons.add(cancel);
			buttons.add(Box.createHorizontalGlue());

			buttons.setAlignmentX(Component.CENTER_ALIGNMENT);
			custom.add(buttons, BorderLayout.SOUTH);

			Border border = BorderFactory.createEmptyBorder(10, 10, 10, 10);
			custom.setBorder(border);

			getContentPane().setLayout(new BorderLayout());
			getContentPane().add(custom, BorderLayout.CENTER);

			getRootPane().setDefaultButton(okay);

			//
			okay.addActionListener(new OkayAction());
			cancel.addActionListener(new CancelAction());

			pack();
			
			setLocationRelativeTo(getOwner());

		}

		private class OkayAction extends AbstractAction {
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				//hide();
				setVisible(false);
				option = APPROVE_OPTION;
			}
		}

		private class CancelAction extends AbstractAction {
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				//hide();
				setVisible(false);
				option = CANCEL_OPTION;
			}
		}

		public void setMessage(String msg) {
			message.setText("<html><font>"+msg+"</font></html>");
		}
		

		public void setText(String msg) {
			message.setText(msg);
		}
	}

	public static int confirm(Frame owner, String msg, String title) {
		ConfirmDialog md = new ConfirmDialog(owner, true);
		md.setTitle(title);
		md.setMessage(msg);
		return md.showDialog();
	}
}
